package com.dduggs.note.util;

import java.security.MessageDigest;

public class Md5Util {
		/**
	    * 
	    * @param str(ex: 明碼字串)
	    * @return md5(加密後字串)
	    */
		public static String changeToMd5(String str){
			String md5 = null;
			try{
					MessageDigest md = MessageDigest.getInstance("MD5");
					md.update(str.getBytes());
					md5 = toHexString(md.digest());
				} catch (Exception e) {
					e.printStackTrace();
				}
			return md5;
		}
		/**
		 *
		 * md5密碼(byte)
		 * @return md5密碼(16進位)
		 */
		private static String toHexString(byte[] in) {
			StringBuilder hexString = new StringBuilder();
			for (int i = 0; i < in.length; i++){
				String hex = Integer.toHexString(0xFF & in[i]);
				if (hex.length() == 1){
					hexString.append('0');
				}
				hexString.append(hex);
			}
			return hexString.toString();
		}
}
